<template>
  <!-- 面包屑 -->
  <div>
    <el-breadcrumb :separator-icon="ArrowRight">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>报销补帖</el-breadcrumb-item>
      <el-breadcrumb-item>补贴管理</el-breadcrumb-item>
      <el-breadcrumb-item><b>支付申请</b></el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <!-- 搜索框 -->
  <div style="margin:40px 20px 20px 20px">
    <el-form :inline="true" ref="searchFormRef" :model="searchFormModel" class="demo-form-inline" style="margin-top: 20px">
      <el-form-item label="部门" prop="department">
        <el-select v-model="searchFormModel.department" placeholder="请选择申请部门" clearable style="width: 130px">
          <el-option
              v-for="item in departmentOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="经办人" prop="transactor">
        <el-input v-model="searchFormModel.transactor" placeholder="请输入经办人" clearable />
      </el-form-item>
      <el-form-item label="收款人" prop="payee">
        <el-input v-model="searchFormModel.payee" placeholder="请输入收款人" clearable />
      </el-form-item>
      <el-form-item label="日期" prop="month">
        <el-date-picker clearable
                        v-model="searchFormModel.month"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择日期"
                       >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审批状态" prop="state">
        <el-select v-model="searchFormModel.state" placeholder="请选择审批状态" clearable style="width: 150px">
          <el-option
              v-for="item in stateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="searchOnSubmit">查询</el-button>
      </el-form-item>
    </el-form>
  </div>
<!--数据表格-->
  <div style="margin:20px">
  <el-table :data="blogInfo" stripe border style="width: 100%">
    <el-table-column label="申请部门" align="center" prop="department" />
    <el-table-column label="经办人" align="center" prop="transactor" />
    <el-table-column label="收款人" align="center" prop="payee" />
    <el-table-column label="付款金额" align="center" prop="paymoney" />
    <el-table-column label="类型" align="center" prop="type"/>
    <el-table-column label="月份" align="center" prop="month" width="180"/>
    <!--      <el-table-column label="审批状态" align="center" prop="state" />-->
    <el-table-column label="审批状态" align="center" prop="state">
      <template #default="scope">
        <span v-if="scope.row.state===0"><el-tag type="danger">已拒绝</el-tag></span>
        <span v-if="scope.row.state===1"><el-tag type="success">已通过</el-tag></span>
        <span v-if="scope.row.state===2"><el-tag type="danger">已驳回</el-tag></span>
        <span v-if="scope.row.state===3"><el-tag type="danger">已撤销</el-tag></span>
        <span v-if="scope.row.state===4"> <el-tag type="warning">审批中</el-tag></span>
      </template>
    </el-table-column>
    <el-table-column label="操作" width="160">
      <template #default="scope">
        <el-button
            size="small"
            @click="tableCheckHandler(scope.$index, scope.row)">
          <el-icon><DocumentChecked /></el-icon>审批
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  </div>
  <!--  分页-->
  <div class="page">
    <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[3,5]"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="sizes, prev, pager, next"
        :total="totals"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
  <!-- 添加模态框 -->
  <el-dialog v-model="dialogAddState" title="添加支付报销申请" width="680">
    <div>
      <el-form ref="formAddRef" :model="formAdd" label-width="120px">
        <el-form-item label="申请部门" prop="type">
          <el-select v-model="formAdd.department" placeholder="请选择申请部门" clearable style="width: 200px;">
            <el-option label="人事部" value="人事部"></el-option>
            <el-option label="后勤部" value="后勤部"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="经办人" prop="transactor">
          <el-input v-model="formAdd.transactor" placeholder="请输入经办人" />
        </el-form-item>
        <el-form-item label="收款人" prop="payee">
          <el-input v-model="formAdd.payee" placeholder="请输入收款人" />
        </el-form-item>
        <el-form-item label="付款金额" prop="paymoney">
          <el-input v-model="formAdd.paymoney" placeholder="请输入付款金额" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="formAdd.type" placeholder="请选择类型" clearable >
            <el-option label="员工加班补助" value="员工加班补助"></el-option>
            <el-option label="人民陪审补助" value="人民陪审补助"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="月份" prop="month">
          <el-date-picker clearable
                          v-model="formAdd.month"
                          type="date"
                          placeholder="请选择日期"
                          @change="formatDate1"
                          >
          </el-date-picker>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogAddOnReset">取消</el-button>
        <el-button type="primary" @click="submitRegisterForm">确认</el-button>
      </div>
    </template>
  </el-dialog>
  <!--  编辑模态框-->
  <el-dialog v-model="dialogEditState" title="修改支付报销申请" width="700">
    <div>
      <el-form ref="formEditRef" :model="formEdit" label-width="120px">
        <el-form-item label="申请部门" prop="type">
          <el-select v-model="formEdit.department" placeholder="请选择申请部门" clearable style="width: 200px;">
            <el-option label="人事部" value="人事部"></el-option>
            <el-option label="后勤部" value="后勤部"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="经办人" prop="transactor">
          <el-input v-model="formEdit.transactor" placeholder="请输入经办人" />
        </el-form-item>
        <el-form-item label="收款人" prop="payee">
          <el-input v-model="formEdit.payee" placeholder="请输入收款人" />
        </el-form-item>
        <el-form-item label="付款金额" prop="paymoney">
          <el-input v-model="formEdit.paymoney" placeholder="请输入付款金额" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="formEdit.type" placeholder="请选择类型" clearable >
            <el-option label="员工加班补助" value="员工加班补助"></el-option>
            <el-option label="人民陪审补助" value="人民陪审补助"></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item prop="month" label="月份">
          <el-input v-model="formEdit.month" maxlength="10" ></el-input>
        </el-form-item>-->
        <el-form-item label="月份" prop="month">
          <el-date-picker clearable
                          v-model="formEdit.month"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogEditOnReset">取消</el-button>
        <el-button type="primary"  @click="dialogEditOnSubmit" >更新</el-button>
      </div>
    </template>
  </el-dialog>
  <!--审批模态框-->
  <el-dialog v-model="dialogCheckState" title="请选择审批状态" width="700" >
    <div>
      <el-form ref="formCheckRef" :model="formCheck" label-width="120px">
        <el-form-item prop="state" label="审批状态">
          <template #default="scope">
            <div>
              <el-radio-group v-model="formCheck.state" :disabled="formCheck.state === 1 || formCheck.state === 0 || formCheck.state === 3">
                <el-radio value="1" size="large" border >通过</el-radio>
                <el-radio value="0" size="large" border >拒绝</el-radio>
                <el-radio value="2" size="large" border :disabled="formCheck.state === 2">驳回</el-radio>
              </el-radio-group>
            </div>
          </template>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogCheckOnReset">取消</el-button>
        <el-button type="primary"  @click="dialogCheckOnSubmit" >确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import {ArrowRight} from "@element-plus/icons-vue";
import {Delete, Edit, Plus , DocumentChecked } from "@element-plus/icons";
import {onMounted, reactive, ref} from "vue";
import request from "@/utils/reimburse";
import {ElMessage, ElMessageBox, UploadProps, UploadUserFile} from "element-plus";
import axios from "axios";

const  mallInfo=ref([])
onMounted(()=>{
  getPayapply();
})

//分页
const currentPage = ref(1)
const pageSize = ref(3)
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
let totals=ref(0);

const  blogInfo=ref([])
const formatDate=(value)=> {
  if (!value) {
    // 如果日期时间选择器的值为空，则不进行格式化
    searchFormModel.month = '';
    return;
  }
  // 将日期时间字符串转换为Date对象
  const date = new Date(value);
  // 获取年、月、日、小时、分钟和秒
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const day = date.getDate().toString().padStart(2, '0');

  // 组合成所需的格式
  searchFormModel.month = `${year}-${month}-${day}`;
}

// 表格显示内容
const getPayapply = () => {
  const params = {
    pageNum: currentPage.value,
    pageSize: pageSize.value,
    // 初始时不包含ID参数
  };
  // 如果申请部门不为空，则添加到查询参数中
  if (searchFormModel.department && searchFormModel.department.trim() !== '') {
    params.department = searchFormModel.department.trim();
  }
  // 如果经办人不为空，则添加到查询参数中
  if (searchFormModel.transactor && searchFormModel.transactor.trim() !== '') {
    params.transactor = searchFormModel.transactor.trim();
  }
  // 如果收款人不为空，则添加到查询参数中
  if (searchFormModel.payee && searchFormModel.payee.trim() !== '') {
    params.payee = searchFormModel.payee.trim();
  }
  // 如果类型不为空，则添加到查询参数中
  if (searchFormModel.type && searchFormModel.type.trim() !== '') {
    params.type = searchFormModel.type.trim();
  }
  // 如果上传票据不为空，则添加到查询参数中
  if (searchFormModel.month && searchFormModel.month.trim() !== '') {
    params.month = searchFormModel.month.trim();
  }
  // 如果审批状态不为空，则添加到查询参数中
  if (searchFormModel.state && searchFormModel.state.trim() !== '') {
    params.state = searchFormModel.state.trim();
  }
  request({
    url: '/payapply/payapplyList',
    method: 'get',
    params,
  }).then((req) => {
    console.log(req)
    if (200 == req.data.errorCode) {
      blogInfo.value = req.data.data.records.map(item => ({
        ...item,
      }));
      totals.value = req.data.data.total;
    }
  })
}
const handleCurrentChange = (page) => {
  currentPage.value = page
  getPayapply()
}
const handleSizeChange = (size) => {
  pageSize.value = size
  getPayapply()
}

//搜索
const searchFormRef=ref()
const searchFormModel=reactive({
  id:'',
  department:'',
  transactor:'',
  payee:'',
  paymoney:'',
  type:'',
  month:'',
  state:''
})

// 定义下拉框选项
const departmentOptions = ref([
  {value: '', label: '全部'},
  { value: '人事部', label: '人事部' },
  { value: '后勤部', label: '后勤部' },
]);

const stateOptions = ref([
  {value: '', label: '全部'},
  { value: '0', label: '已拒绝' },
  { value: '1', label: '已通过' },
  { value: '2', label: '已驳回' },
  { value: '3', label: '已撤销' },
  { value: '4', label: '审批中' },
]);


const searchOnSubmit=()=>{
  getPayapply()
}
const searchOnReset=()=>{
  searchFormModel.transactor=''
}
///////////////////添加/////////////////////////
const dialogAddState = ref(false);
const formAddData = {
  department:'',
  transactor:'',
  payee:'',
  paymoney:'',
  type:'',
  month:'',
  state:''
};
const formAdd = reactive({...formAddData});
const addressFormRef = ref(null); // 创建一个ref引用form

// 提交表单
function submitRegisterForm (){
  request({
    method:'POST',
    url:'/payapply/add',
    params:formAdd
  }).then(req=>{
    if (200 == req.data.errorCode){
      ElMessage({
        type: 'success',
        message: '新增成功',
      });
      dialogAddState.value=false;
      getPayapply()
    }else {
      ElMessage({
        type: 'info',
        message: '新增失败',
      });
    }
   /* ElMessage({
      type:'info',
      message:req.data.msg
    })
    if (200 == req.data.errorCode){
      Object.assign(formAdd,formAddData)
    }*/
  })
 /* dialogAddState.value=false;*/
}
const formatDate1=(value)=> {
  if (!value) {
    // 如果日期时间选择器的值为空，则不进行格式化
    formAdd.month = '';
    return;
  }
  // 将日期时间字符串转换为Date对象
  const date = new Date(value);
  // 获取年、月、日、小时、分钟和秒
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const day = date.getDate().toString().padStart(2, '0');

  // 组合成所需的格式
  formAdd.month = `${year}-${month}-${day} `;
}
const dialogAddOnReset = () => {
  formAdd.department=''
  formAdd.transactor=''
  formAdd.payee=''
  formAdd.paymoney=''
  formAdd.type=''
  formAdd.month=''
  formAdd.state=''
  // 关闭对话框
  dialogAddState.value=false;
}
///////////////////编辑/////////////////////////
const tableEditHandler = (rowIndex , rowData) => {
  console.log("-------------")
  console.log(rowIndex)
  console.log(rowData)

  formEdit.id = rowData.id
  formEdit.department = rowData.department
  formEdit.transactor = rowData.transactor
  formEdit.payee = rowData.payee
  formEdit.paymoney = rowData.paymoney
  formEdit.type = rowData.type
  formEdit.month = rowData.month
  formEdit.state = rowData.state


  dialogEditState.value = true
}
// 新增表单引用
const formEditRef = ref()
const dialogEditState = ref(false)
const dialogEditOnSubmit = () => {
  // 数据提交到服务器
  // 服务器端返回成功 ：清空表单，关闭对话框，更新表格内容
  request({
    method:'PUT',
    url:'/payapply/update',
    data:JSON.stringify(formEdit),
    headers: {'content-type': 'application/json;charset=UTF-8'}
  }).then(req=>{
    ElMessage({
      type:'success',
      message:req.data.msg
    })
    if (200==req.data.errorCode){
      getPayapply()
    }
  })
  dialogEditState.value = false
}
// 新增表单初始化数据
const formEditData = {
  id:'',
  department:'',
  transactor:'',
  payee:'',
  paymoney:'',
  type:'',
  month:'',
  state:''
}
const dialogEditOnReset = () => {
  formEdit.department=''
  formEdit.transactor=''
  formEdit.payee=''
  formEdit.paymoney=''
  formEdit.type=''
  formEdit.month=''
  formEdit.state=''
  // 关闭对话框
  dialogEditState.value=false;
}
// 编辑表单绑定数据模型
const formEdit = reactive({...formEditData})

///////////////////////审批/////////////////////////////////
const formCheckRef = ref()
const dialogCheckState = ref(false)

// 新增表单初始化数据
const formCheckData = {
  id:'',
  state:''
}
const tableCheckHandler = (rowIndex , rowData) => {
  console.log("-------------")
  console.log(rowIndex)
  console.log(rowData)

  formCheck.id = rowData.id
  formCheck.state = rowData.state


  dialogCheckState.value = true
}
const dialogCheckOnReset = () => {
  formEdit.state=''
  // 关闭对话框
  dialogCheckState.value=false;
}
const dialogCheckOnSubmit = () => {
  // 数据提交到服务器
  // 服务器端返回成功 ：清空表单，关闭对话框，更新表格内容
  request({
    method:'PUT',
    url:'/payapply/update',
    data:JSON.stringify(formCheck),
    headers: {'content-type': 'application/json;charset=UTF-8'}
  }).then(req=>{
    ElMessage({
      type:'success',
      message:req.data.msg
    })
    if (200==req.data.errorCode){
      getPayapply()
    }
  })
  dialogCheckState.value = false
}

const formCheck = reactive({...formCheckData})
</script>

<style scoped>
.el-breadcrumb{
  font-size: 18px;
  margin: 20px;
}
</style>